##-----------------------------------------##
## Lauren Peritz
## ISQ 2019 - WTO Compliance
## Replication File Part 4
## Use "Peritz_ISQ2019_WTO4.dta"              
## Update: 2019.11.19                                 
###########################################

#################################################################################
#### 1: Preliminaries -----------------------------------------------------------

rm(list=ls(all=TRUE))

# Load packages
library(Synth)
library(reshape)
library(foreign)
library(scales)
library(xtable)
library(haven)


# Set working directory
setwd("~/Dropbox/WTO_Compliance/Replication")

# Import data
dat <- as.data.frame(read_dta("~/Dropbox/WTO_Compliance/Replication/Peritz_ISQ2019_WTO4.dta"))


#################################################################################
#### 2: Prepare SCM Calculations ------------------------------------------------


# Identify disputes
ds.list <- levels(as.factor(dat$Dispute))

# Create Economic Compliance Output Matrix
Comply <-as.data.frame(matrix(NA, length(ds.list), 3))
Comply[,1] <- ds.list
colnames(Comply)<- c("Dispute","comply_scm","comply_sd")


# Custom color for plots
tr.gray <- alpha("gray", alpha=.3)

# Default predictors (covariates) to use in SCM
pred.default <- c("gdp","gdp_growth","gdppc","agriculture_value_added",  
                  "industry_value_added","manufacturing_value_added",
                  "services_value_added","trade","unemployment_t")

# many missing values for manufacturing, omit where needed
pred.2 <- pred.default[-6]	

# Can easily add "polity" to predictor list for robustness check


#################################################################################
#### 3: Functions ------------------------------------------------


## scm.analysis()

scm.analysis <- function (DS.No, Resp, dat, predictors.list, window = 5){
  
  # Parameters
  DS <- dat[dat$Dispute==DS.No,]
  
  year.min <- min(DS$Year)
  year.max <- min((mean(DS$RPTExpireYear)+window), max(DS$Year))
  year.rpt <- mean(DS$RPTExpireYear)
  donor.pool <- unique(DS$PartnerCode[DS$PartnerCode!=Resp])
  
  # subset Dataset
  DS <- DS[DS$Year<=year.max,]
  attach(DS)
  
  # Compute Log-Odds as log(Share.Respondent/Share.Next.Largest)
  DS.sub <- DS[DS$PartnerCode!=Resp,c(2,5,11)]
  control.means <- by(DS.sub$Share, INDICES=as.factor(DS.sub$PartnerCode), FUN = mean)
  denom <<- DS.sub[DS.sub$PartnerCode==labels(control.means[control.means==max(control.means)]),]
  denom.long <- rep(denom$Share,length(levels(as.factor(DS$PartnerCode))))
  DS$l.odds.rat <- log(DS$Share/denom.long)
  
  # SCM calculation
  dataprep.out <<- dataprep(foo=DS, 
                            predictors=predictors.list,
                            dependent = "l.odds.rat", 
                            unit.variable = "PartnerCode", time.variable = "Year",
                            treatment.identifier = Resp, 
                            controls.identifier = donor.pool,
                            time.predictors.prior = c(year.min:year.rpt),   
                            time.optimize.ssr = c(year.min:year.rpt),
                            time.plot = (year.min:year.max),
                            unit.names.variable = "PartnerName")
  synth.out <<- synth(dataprep.out)

  # Summary tables 
  synth.tables <<- synth.tab(dataprep.res = dataprep.out,synth.res = synth.out)
  print(synth.tables)

}

## ds.stat()
## Calculates compliance statistic, transformed to trade share

ds.stat<-function(DS.No){
  	all <-length(dataprep.out$tag$time.plot)
  	c <-length(dataprep.out$tag$time.predictors.prior)
	  gaps <- exp(dataprep.out$Y1plot)*denom$Share-exp(dataprep.out$Y0plot%*%synth.out$solution.w)*denom$Share
  	gaps.diff<-round(mean(gaps[c:all,])-mean(gaps[1:c,]),6)
  	gaps.sd <- (round( sd(as.vector(gaps[1:c,]))  ,6))/2
  	stat <- as.data.frame(cbind(DS.No, gaps.diff, gaps.sd))
  	print(stat)
  	return(stat)
	}# end ds.stat()


	
## path.plot.exp() 
## Plots SCM, transformed back to trade share

path.plot.exp<-function (synth.res = NA, dataprep.res = NA, tr.intake = NA, 
    Ylab = c("Y Axis"), Xlab = c("Time"), Ylim = NA, Legend = c("Treated", 
        "Synthetic"), Legend.position = c("topright"), Main = NA, 
    Z.plot = FALSE, cex.l=6/7) 
{
    if (Z.plot == FALSE) {
        if (sum(is.na(dataprep.res$Y1plot)) > 0) {
            stop("\n\n#####################################################", 
                "\nYou have missing Y data for the treated!\n\n")
        }
        if (sum(is.na(dataprep.res$Y0plot)) > 0) {
            stop("\n\n#####################################################", 
                "\nYou have missing Y data for the controls!\n\n")
        }
        y0plot1 <- dataprep.res$Y0plot %*% synth.res$solution.w
        if (sum(is.na(Ylim)) > 0) {
            Y.max <- max(c(y0plot1, dataprep.res$Y1plot))
            Y.min <- min(c(y0plot1, dataprep.res$Y1plot))
            Ylim <- c((Y.min - 0.3 * Y.min), (0.3 * Y.max + Y.max))
        }
        plot(dataprep.res$tag$time.plot, exp(dataprep.res$Y1plot)*denom$Share, 
            t = "l", col = "black", lwd = 2, main = Main, ylab = Ylab, 
            xlab = Xlab, xaxs = "i", yaxs = "i", ylim = Ylim)
        lines(dataprep.res$tag$time.plot, exp(y0plot1)*denom$Share, col = "black", 
            lty = "dashed", lwd = 2, cex = 4/5)
    }
    else {
        z0plot <- dataprep.res$Z0 %*% synth.res$solution.w
        if (sum(is.na(Ylim)) > 0) {
            Y.max <- max(c(z0plot, dataprep.res$Z1))
            Y.min <- min(c(z0plot, dataprep.res$Z1))
            Ylim <- c((Y.min - 0.3 * Y.min), (0.3 * Y.max + Y.max))
        }
        plot(dataprep.res$tag$time.optimize.ssr, exp(z0plot)*denom$Share, t = "l", 
            col = "black", lwd = 2, main = Main, ylab = Ylab, 
            xlab = Xlab, xaxs = "i", yaxs = "i", ylim = Ylim)
        lines(dataprep.res$tag$time.optimize.ssr, exp(dataprep.res$Z1)*denom$Share, 
            col = "black", lty = "dashed", lwd = 2, cex = 4/5)
    }
    abline(v = tr.intake, lty = 3, col = "black", lwd = 2)
    if (sum(is.na(Legend)) == 0) {
        legend(Legend.position, legend = Legend, lty = 1:2, col = c("black", 
            "black"), lwd = c(2, 2), cex = cex.l, bty="n")
    }
}



#################################################################################
#### 4: SCM for Each Dispute (By DS.No) --------------------


## DS2: Venezuela v. US - Gasoline     
scm.analysis(DS.No=2, Resp=842, dat=dat, predictors.list=pred.2)
Comply[Comply$Dispute==2,2:3]<-ds.stat(DS.No=2)[2:3]
detach(DS)

## DS4: Brazil v. US - Gasoline      
scm.analysis(DS.No=4, Resp=842, dat=dat, predictors.list=pred.2)
Comply[Comply$Dispute==4,2:3]<-ds.stat(DS.No=4)[2:3]
detach(DS)


## DS8: EU v. Japan - Alcoholic Beverages	
scm.analysis(DS.No=8, Resp=392, dat=dat, predictors.list=c(pred.default,"polity"))
Comply[Comply$Dispute==8,2:3]<-ds.stat(DS.No=8)[2:3]
detach(DS)

## Plot FIG2b_ds8
par(mar=c(5,5,6,2))
path.plot.exp(dataprep.res = dataprep.out,synth.res = synth.out, 
       Ylim = c(-.015, .10),
	   Ylab = "EU Alcohol Exports to Japan (Share)",
       Xlab = "Year",
       Legend = c("Japan", "Synthetic Japan"))
rect(xleft=1995+5/12, xright=1998+1/12,ybottom=-0.015, ytop=.10, col=tr.gray, border = tr.gray)
abline(v=1998+1/12, lwd=2, col="grey35")
mtext("EU v. Japan - Alcoholic Beverages (DS8)", side=3, line=1, cex=1.4)

## DS10: Canada v. Japan - Alcoholic Beverages   
scm.analysis(DS.No=10, Resp=392, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==10,2:3]<-ds.stat(10)[,2:3]
detach(DS)

## DS11: US v. Japan - Alcoholic Beverages   
scm.analysis(DS.No=11, Resp=392, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==11,2:3]<-ds.stat(11)[,2:3]
detach(DS)

## DS18: Canada v. Australia - Salmon  
scm.analysis(DS.No=18, Resp=36, dat=dat, predictors.list=pred.2)
Comply[Comply$Dispute==18,2:3]<-ds.stat(18)[2:3]
detach(DS)

## DS24: Costa Rica v. US - Underwear  
scm.analysis(DS.No=24, Resp=842, dat=dat, predictors.list=pred.2)
Comply[Comply$Dispute==24,2:3]<-ds.stat(24)[2:3]
detach(DS)

## DS26: US v. EU - Hormones  
scm.analysis(DS.No=26, Resp=97, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==26,2:3]<-ds.stat(26)[2:3]
detach(DS)

## DS27: Ecuador, Guatemala, Honduras, Mexico, US v. EU - Bananas III 		
scm.analysis(DS.No=27, Resp=97, dat=dat, predictors.list=pred.2)
Comply[Comply$Dispute==27,2:3]<-ds.stat(27)[2:3]
detach(DS)

## DS31: US v. Canada - Periodicals	
scm.analysis(DS.No=31, Resp=124, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==31,2:3]<-ds.stat(31)[2:3]
detach(DS)

## DS33: India v. US - Wool			
scm.analysis(DS.No=33, Resp=842, dat=dat, predictors.list=pred.2)
Comply[Comply$Dispute==33,2:3]<-ds.stat(33)[2:3]
detach(DS)

## DS34: India v. Turkey - Textiles	  
scm.analysis(DS.No=34, Resp=792, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==34,2:3]<-ds.stat(34)[2:3]
detach(DS)

## DS48: Canada v. EU - Hormones	
scm.analysis(DS.No=48, Resp=97, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==48,2:3]<-ds.stat(48)[2:3]
detach(DS)

## DS50: US v. India - Patents   
scm.analysis(DS.No=50, Resp=699, dat=dat, predictors.list=pred.2)
Comply[Comply$Dispute==50,2:3]<-ds.stat(50)[2:3]
detach(DS)

## DS56: US v. Argentina - Textile	
scm.analysis(DS.No=56, Resp=32, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==56,2:3]<-ds.stat(56)[2:3]
detach(DS)

## DS62: US v. EU - Computer Equipment	
scm.analysis(DS.No=62, Resp=97, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==62,2:3]<-ds.stat(62)[2:3]
detach(DS)

## DS67: US v. UK - Computers    
scm.analysis(DS.No=67, Resp=826, dat=dat, predictors.list = pred.default[-4])
Comply[Comply$Dispute==67,2:3]<-ds.stat(67)[2:3]
detach(DS)

## DS68: US v. Ireland - Computers  
scm.analysis(DS.No=68, Resp=372, dat=dat, predictors.list = pred.default[-4])
Comply[Comply$Dispute==68,2:3]<-ds.stat(68)[2:3]
detach(DS)

## DS69: Brazil v. EU - Poultry  	
scm.analysis(DS.No=69, Resp=97, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==69,2:3]<-ds.stat(69)[2:3]
detach(DS)

## DS75: EU v. Korea - Alcoholic Beverages  
scm.analysis(DS.No=75, Resp=410, dat=dat, predictors.list=c(pred.default,"polity"))
Comply[Comply$Dispute==75,2:3]<-ds.stat(75)[2:3]
detach(DS)

## Plot FIG2a_ds75
 par(mar=c(5,5,6,2))
 path.plot.exp(dataprep.res = dataprep.out,synth.res = synth.out, 
               Ylim = c(0, .12),
               Ylab = "EU Alcohol Exports to Korea (Share)",
               Xlab = "Year",
               Legend = c("Korea", "Synthetic Korea"),
               Legend.position = "topleft")
 rect(xleft=1997+3/12, xright=2000+1/12,ybottom=0, ytop=.12, col=tr.gray, border = tr.gray)
 abline(v=2000+1/12, lwd=2, col="grey35")
 mtext("EU v. Korea - Alcoholic Beverages (DS75)", side=3, line=1, cex=1.2)


## DS76: US v. Japan - Agricultural Products II  
scm.analysis(DS.No=76, Resp=392, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==76,2:3]<-ds.stat(76)[2:3]
detach(DS)

## DS79: EU v. India - Patents  	
scm.analysis(DS.No=79, Resp=699, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==79,2:3]<-ds.stat(79)[2:3]
detach(DS)

## DS84: US v. Korea - Alcoholic Beverages  
scm.analysis(DS.No=84, Resp=410, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==84,2:3]<-ds.stat(84)[2:3]
detach(DS)

## DS87: EU v. Chile - Alcoholic Beverages  
scm.analysis(DS.No=87, Resp=152, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==87,2:3]<-ds.stat(87)[2:3]
detach(DS)

## DS90: US v. India - Quantitative Restrictions
scm.analysis(DS.No=90, Resp=699, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==90,2:3]<-ds.stat(90)[2:3]
detach(DS)

## DS98 - EU v. Korea - Dairy	
scm.analysis(DS.No=98, Resp=410, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==98,2:3]<-ds.stat(98)[2:3]
detach(DS)

## DS99: Korea v. US - DRAMS		
scm.analysis(DS.No=99, Resp=842, dat=dat, predictors.list=pred.2)
Comply[Comply$Dispute==99,2:3]<-ds.stat(99)[2:3]
detach(DS)

## DS103: US v. Canada - Dairy			
scm.analysis(DS.No=103, Resp=124, dat=dat, predictors.list=pred.2)
Comply[Comply$Dispute==103,2:3]<-ds.stat(103)[2:3]
detach(DS)

## DS108: EU v. US - FSC			
scm.analysis(DS.No=108, Resp=842, dat=dat, predictors.list=pred.2)
Comply[Comply$Dispute==108,2:3]<-ds.stat(108)[2:3]
detach(DS)

## DS110: EU v. Chile - Alcoholic Beverages  
scm.analysis(DS.No=110, Resp=152, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==110,2:3]<-ds.stat(110)[2:3]
detach(DS)

## DS114: EU v. Canada - Pharmaceuticals  
scm.analysis(DS.No=114, Resp=124, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==114,2:3]<-ds.stat(114)[2:3]
detach(DS)

## DS121: EU v. Argentina - Footwear	
scm.analysis(DS.No=121, Resp=32, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==121,2:3]<-ds.stat(121)[2:3]
detach(DS)

## DS122: Poland v. Thailand - Steel		
scm.analysis(DS.No=122, Resp=764, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==122,2:3]<-ds.stat(122)[2:3]
detach(DS)

## DS132: USA v. Mexico - Corn Syrup		
scm.analysis(DS.No=132, Resp=484, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==132,2:3]<-ds.stat(132)[2:3]
detach(DS)

## DS135: Canada v. EU - Asbestos		
scm.analysis(DS.No=135, Resp=97, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==135,2:3]<-ds.stat(135)[2:3]
detach(DS)

## DS136: EU v. US - 1916 Act		
scm.analysis(DS.No=136, Resp=842, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==136,2:3]<-ds.stat(136)[2:3]
detach(DS)

## DS138: EU v. US - Lead & Bismuth	
scm.analysis(DS.No=138, Resp=842, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==138,2:3]<-ds.stat(138)[2:3]
detach(DS)

## DS141: India v. EU - Linen		
scm.analysis(DS.No=141, Resp=97, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==141,2:3]<-ds.stat(141)[2:3]
detach(DS)

## DS146: EU v. India - Autos		
scm.analysis(DS.No=146, Resp=699, dat=dat, predictors.list=pred.default[-9])
Comply[Comply$Dispute==146,2:3]<-ds.stat(146)[2:3]
detach(DS)

## DS156: Mexico v Guatemala - Cement II  
scm.analysis(DS.No=156, Resp=320, dat=dat, predictors.list=pred.default[-c(4:7)])
Comply[Comply$Dispute==156,2:3]<-ds.stat(156)[2:3]
detach(DS)

## DS160: EU v. US - Section 110(5) Copyright Act	
scm.analysis(DS.No=160, Resp=842, dat=dat, predictors.list=pred.2)
Comply[Comply$Dispute==160,2:3]<-ds.stat(160)[2:3]
detach(DS)

## DS161: US v. Korea - Meat		
scm.analysis(DS.No=161, Resp=410, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==161,2:3]<-ds.stat(161)[2:3]
detach(DS)

## DS162: Japan v. US - 1916 Act		
scm.analysis(DS.No=162, Resp=842, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==162,2:3]<-ds.stat(162)[2:3]
detach(DS)

## DS166: EU v. US - Wheat Gluten		
scm.analysis(DS.No=166, Resp=842, dat=dat, predictors.list=pred.2)
Comply[Comply$Dispute==166,2:3]<-ds.stat(166)[2:3]
detach(DS)

## DS169: Australia v. Korea - Meat  
scm.analysis(DS.No=169, Resp=410, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==169,2:3]<-ds.stat(169)[2:3]
detach(DS)

## DS170: US v. Canada - Patent Term  	
scm.analysis(DS.No=170, Resp=124, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==170,2:3]<-ds.stat(170)[2:3]
detach(DS)

## DS175: US v. India - Autos  	
scm.analysis(DS.No=175, Resp=699, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==175,2:3]<-ds.stat(175)[2:3]
detach(DS)

## DS176: EU v. US - Section 211 Appropriations Act  
scm.analysis(DS.No=176, Resp=842, dat=dat, predictors.list=pred.2)
Comply[Comply$Dispute==176,2:3]<-ds.stat(176)[2:3]
detach(DS)

## DS177: New Zealand v. US - Lamb	
scm.analysis(DS.No=177, Resp=842, dat=dat, predictors.list=pred.2)
Comply[Comply$Dispute==177,2:3]<-ds.stat(177)[2:3]
detach(DS)

## DS178: Australia v. US - Lamb		
scm.analysis(DS.No=178, Resp=842, dat=dat, predictors.list=pred.2)
Comply[Comply$Dispute==178,2:3]<-ds.stat(178)[2:3]
detach(DS)

## DS179: Korea v. US - Steel		
scm.analysis(DS.No=179, Resp=842, dat=dat, predictors.list=pred.2)
Comply[Comply$Dispute==179,2:3]<-ds.stat(179)[2:3]
detach(DS)

## DS184: Japan v. US - Hot Rolled Steel  
scm.analysis(DS.No=184, Resp=842, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==184,2:3]<-ds.stat(184)[2:3]
detach(DS)

## DS189: EU v. Argentina - Ceramic Tiles	
scm.analysis(DS.No=189, Resp=32, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==189,2:3]<-ds.stat(189)[2:3]
detach(DS)

## DS202: Korea v. US - Line Pipe   	
scm.analysis(DS.No=202, Resp=842, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==202,2:3]<-ds.stat(202)[2:3]
detach(DS)

## DS204: US v. Mexico - Telecommunication	
scm.analysis(DS.No=204, Resp=484, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==204,2:3]<-ds.stat(204)[2:3]
detach(DS)

## DS207: Argentina v. Chile - Price Band System  
scm.analysis(DS.No=207, Resp=152, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==207,2:3]<-ds.stat(207)[2:3]
detach(DS)

## DS211: Turkey v. Egypt - Steel Rebar		
scm.analysis(DS.No=211, Resp=818, dat=dat, predictors.list=pred.default[-c(4:7)])
Comply[Comply$Dispute==211,2:3]<-ds.stat(211)[2:3]
detach(DS)

## DS212: EU v. US - CVD on Certain EC Products	
scm.analysis(DS.No=212, Resp=842, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==212,2:3]<-ds.stat(212)[2:3]
detach(DS)

## DS213: EU (Germany) v. US - Carbon Steel 
scm.analysis(DS.No=213, Resp=842, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==213,2:3]<-ds.stat(213)[2:3]
detach(DS)

## DS219: Brazil v. EU - Tube and Pipe Fittings 
scm.analysis(DS.No=219, Resp=97, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==219,2:3]<-ds.stat(219)[2:3]
detach(DS)

## DS231: Peru v. EU - Sardines		
scm.analysis(DS.No=231, Resp=97, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==231,2:3]<-ds.stat(231)[2:3]
detach(DS)

## DS236: Canada v. US - Softwood lumber III  
scm.analysis(DS.No=236, Resp=842, dat=dat, predictors.list=pred.default[-4])
Comply[Comply$Dispute==236,2:3]<-ds.stat(236)[2:3]
detach(DS)

## DS238: Chile v. Argentina - Peaches    
scm.analysis(DS.No=238, Resp=32, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==238,2:3]<-ds.stat(238)[2:3]
detach(DS)

## DS241: Brazil v. Argentina - Poultry   
scm.analysis(DS.No=241, Resp=32, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==241,2:3]<-ds.stat(241)[2:3]
detach(DS)

## DS244: Japan v. US - Steel Sunset Review	
scm.analysis(DS.No=244, Resp=842, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==244,2:3]<-ds.stat(244)[2:3]
detach(DS)

## DS245: US v. Japan - Apples  		
scm.analysis(DS.No=245, Resp=392, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==245,2:3]<-ds.stat(245)[2:3]
detach(DS)

## DS246: India v. EU - Tariff Preferences   
scm.analysis(DS.No=246, Resp=97, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==246,2:3]<-ds.stat(246)[2:3]
detach(DS)

## DS248: EU v. US - Steel Safeguards	
scm.analysis(DS.No=248, Resp=842, dat=dat, predictors.list=pred.2)
Comply[Comply$Dispute==248,2:3]<-ds.stat(248)[2:3]
detach(DS)

## DS249: Japan v. US - Steel Safeguards  	
scm.analysis(DS.No=249, Resp=842, dat=dat, predictors.list=pred.2)
Comply[Comply$Dispute==249,2:3]<-ds.stat(249)[2:3]
detach(DS)

## DS251: Korea v. US - Steel Safeguards  	
scm.analysis(DS.No=251, Resp=842, dat=dat, predictors.list=pred.2)
Comply[Comply$Dispute==251,2:3]<-ds.stat(251)[2:3]
detach(DS)

## DS252: China v. US - Steel Safeguards  	
scm.analysis(DS.No=252, Resp=842, dat=dat, predictors.list=pred.2)
Comply[Comply$Dispute==252,2:3]<-ds.stat(252)[2:3]
detach(DS)

## DS253: Switzerland v. US - Steel Safeguards  
scm.analysis(DS.No=253, Resp=842, dat=dat, predictors.list=pred.2)
Comply[Comply$Dispute==253,2:3]<-ds.stat(253)[2:3]
detach(DS)

## DS254: Norway v. US - Steel Safeguards  
scm.analysis(DS.No=254, Resp=842, dat=dat, predictors.list=pred.2)
Comply[Comply$Dispute==254,2:3]<-ds.stat(254)[2:3]
detach(DS)

## DS257: Canada v. US - Softwood Lumber IV  	
scm.analysis(DS.No=257, Resp=842, dat=dat, predictors.list=pred.2)
Comply[Comply$Dispute==257,2:3]<-ds.stat(257)[2:3]
detach(DS)

## DS258: New Zealand v. US - Steel Safeguards  
scm.analysis(DS.No=258, Resp=842, dat=dat, predictors.list=pred.2)
Comply[Comply$Dispute==258,2:3]<-ds.stat(258)[2:3]
detach(DS)

## DS259: Brazil v. US - Steel Safeguards  
scm.analysis(DS.No=259, Resp=842, dat=dat, predictors.list=pred.2)
Comply[Comply$Dispute==259,2:3]<-ds.stat(259)[2:3]
detach(DS)

## DS264: Canada v. US - Softwood Lumber V   	
scm.analysis(DS.No=264, Resp=842, dat=dat, predictors.list=pred.2)
Comply[Comply$Dispute==264,2:3]<-ds.stat(264)[2:3]
detach(DS)

## DS268: Argentina v. US - Oil Country Tubular Goods 		
scm.analysis(DS.No=268, Resp=842, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==268,2:3]<-ds.stat(268)[2:3]
detach(DS)

## DS269: Brazil v. EU - Poultry    	
scm.analysis(DS.No=269, Resp=97, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==269,2:3]<-ds.stat(269)[2:3]
detach(DS)

## DS277: Canada v. US - Softwood Lumber VI    
scm.analysis(DS.No=277, Resp=842, dat=dat, predictors.list=pred.2)
Comply[Comply$Dispute==277,2:3]<-ds.stat(277)[2:3]
detach(DS)

## DS282: Mexico v. US - Oil Country Tubular Goods  
scm.analysis(DS.No=282, Resp=842, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==282,2:3]<-ds.stat(282)[2:3]
detach(DS)

## DS286: Thailand v. EU - Poultry  	
scm.analysis(DS.No=286, Resp=97, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==286,2:3]<-ds.stat(286)[2:3]
detach(DS)

## DS290: Australia v. EU - Trademarks and Geographical Indicators  
scm.analysis(DS.No=290, Resp=97, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==290,2:3]<-ds.stat(290)[2:3]
detach(DS)

## DS291: US v. EU - Biotechs  	        
scm.analysis(DS.No=291, Resp=97, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==291,2:3]<-ds.stat(291)[2:3]
detach(DS)

## DS292: Canada v. EU - Biotechs  
scm.analysis(DS.No=292, Resp=97, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==292,2:3]<-ds.stat(292)[2:3]
detach(DS)

## DS293: Argentina v. EU - Biotechs  
scm.analysis(DS.No=293, Resp=97, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==293,2:3]<-ds.stat(293)[2:3]
detach(DS)

## DS294: EU v. US - Zeroing  	
scm.analysis(DS.No=294, Resp=842, dat=dat, predictors.list=pred.2)
Comply[Comply$Dispute==294,2:3]<-ds.stat(294)[2:3]
detach(DS)

## DS295: US v. Mexico - AntiDumping Beef & Rice				
scm.analysis(DS.No=295, Resp=484, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==295,2:3]<-ds.stat(295)[2:3]
detach(DS)

## DS296: Korea v. US - DRAM  		
scm.analysis(DS.No=296, Resp=842, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==296,2:3]<-ds.stat(296)[2:3]
detach(DS)

## DS299 - Korea v. EU - DRAMs  		
scm.analysis(DS.No=299, Resp=97, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==299,2:3]<-ds.stat(299)[2:3]
detach(DS)

## DS302: Honduras v. Dominican Rep. - Cigarettes
scm.analysis(DS.No=302, Resp=214, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==302,2:3]<-ds.stat(302)[2:3]
detach(DS)

## DS308: US v. Mexico - Soft Drinks    
scm.analysis(DS.No=308, Resp=484, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==308,2:3]<-ds.stat(308)[2:3]
detach(DS)

## DS312: Indonesia v. Korea - Paper		
scm.analysis(DS.No=312, Resp=410, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==312,2:3]<-ds.stat(312)[2:3]
detach(DS)

## DS315: US v. EU - Customs Matters  
scm.analysis(DS.No=315, Resp=97, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==315,2:3]<-ds.stat(315)[2:3]
detach(DS)

## DS320: EU v. US - Continued Suspensions
scm.analysis(DS.No=320, Resp=842, dat=dat, predictors.list=pred.2)
Comply[Comply$Dispute==320,2:3]<-ds.stat(320)[2:3]
detach(DS)

## DS322: Japan v. US - Zeroing  	
scm.analysis(DS.No=322, Resp=842, dat=dat, predictors.list=pred.2)
Comply[Comply$Dispute==322,2:3]<-ds.stat(322)[2:3]
detach(DS)

## DS331: Guatemala v. Mexico - Steel Pipes and Tubes
scm.analysis(DS.No=331, Resp=484, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==331,2:3]<-ds.stat(331)[2:3]
detach(DS)

## DS332: EU v. Brazil - Retreaded Tyres  
scm.analysis(DS.No=332, Resp=76, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==332,2:3]<-ds.stat(332)[2:3]
detach(DS)

## DS334: US v. Turkey - Rice  		
scm.analysis(DS.No=334, Resp=792, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==334,2:3]<-ds.stat(334)[2:3]
detach(DS)

## DS335: Ecuador v. US - Shrimp  	
scm.analysis(DS.No=335, Resp=842, dat=dat, predictors.list=pred.2)
Comply[Comply$Dispute==335,2:3]<-ds.stat(335)[2:3]
detach(DS)

## DS336: Korea v. Japan DRAMs  		
scm.analysis(DS.No=336, Resp=392, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==336,2:3]<-ds.stat(336)[2:3]
detach(DS)

## DS337: Norway v. EU - Salmon  		
scm.analysis(DS.No=337, Resp=97, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==337,2:3]<-ds.stat(337)[2:3]
detach(DS)

## DS339: EU v. China - Auto Parts  	
scm.analysis(DS.No=339, Resp=156, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==339,2:3]<-ds.stat(339)[2:3]
detach(DS)

## DS340: US v. China - Auto Parts  	
scm.analysis(DS.No=340, Resp=156, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==340,2:3]<-ds.stat(340)[2:3]
detach(DS)

## DS341: EU v. Mexico  		
scm.analysis(DS.No=341, Resp=484, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==341,2:3]<-ds.stat(341)[2:3]
detach(DS)

## DS342: Canada v. China - Auto Parts  	
scm.analysis(DS.No=342, Resp=156, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==342,2:3]<-ds.stat(342)[2:3]
detach(DS)

## DS343: Thailand v. US - Shrimp  	
scm.analysis(DS.No=343, Resp=842, dat=dat, predictors.list=pred.2)
Comply[Comply$Dispute==343,2:3]<-ds.stat(343)[2:3]
detach(DS)

## DS344: Mexico v. US - Stainless Steel  
scm.analysis(DS.No=344, Resp=842, dat=dat, predictors.list=pred.2)
Comply[Comply$Dispute==344,2:3]<-ds.stat(344)[2:3]
detach(DS)

## DS345: India v. US - Customs Bond Directive  
scm.analysis(DS.No=345, Resp=842, dat=dat, predictors.list=pred.2)
Comply[Comply$Dispute==345,2:3]<-ds.stat(345)[2:3]
detach(DS)

## DS350: EU v. US - Continued Zeroing  
scm.analysis(DS.No=350, Resp=842, dat=dat, predictors.list=pred.2)
Comply[Comply$Dispute==350,2:3]<-ds.stat(350)[2:3]
detach(DS)

## DS360: US v. India - Add'l Import Duties  
scm.analysis(DS.No=360, Resp=699, dat=dat, predictors.list=pred.default[-9])
Comply[Comply$Dispute==360,2:3]<-ds.stat(360)[2:3]
detach(DS)

## DS362 US v. China - IPRs			
scm.analysis(DS.No=362, Resp=156, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==362,2:3]<-ds.stat(362)[2:3]
detach(DS)

## DS363 US v. China - Publications / AV Products	
scm.analysis(DS.No=363, Resp=156, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==363,2:3]<-ds.stat(363)[2:3]
detach(DS)

## DS366: Panama v. Columbia - Ports of Entry	
scm.analysis(DS.No=366, Resp=170, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==366,2:3]<-ds.stat(366)[2:3]
detach(DS)

## DS367: New Zealand v. Australia - Apples	
scm.analysis(DS.No=367, Resp=36, dat=dat, predictors.list=pred.default[-c(4:7)])
Comply[Comply$Dispute==367,2:3]<-ds.stat(367)[2:3]
detach(DS)

## DS375: US v. EU - IT Products			
scm.analysis(DS.No=375, Resp=97, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==375,2:3]<-ds.stat(375)[2:3]
detach(DS)

## DS376: Japan v. EU - IT Products			
scm.analysis(DS.No=376, Resp=97, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==376,2:3]<-ds.stat(376)[2:3]
detach(DS)

## DS383: Thailand v. US - PVC plastic bags   
scm.analysis(DS.No=383, Resp=842, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==383,2:3]<-ds.stat(383)[2:3]
detach(DS)

## DS397: China v. EU - Fastners  	
scm.analysis(DS.No=397, Resp=97, dat=dat, predictors.list=pred.default[-c(4:7)])
Comply[Comply$Dispute==397,2:3]<-ds.stat(397)[2:3]
detach(DS)

## DS402: Korea v. US Zeroing
scm.analysis(DS.No=402, Resp=842, dat=dat, predictors.list=pred.default)
Comply[Comply$Dispute==402,2:3]<-ds.stat(402)[2:3]
detach(DS)



#################################################################################
#### 5. Finish and Export DV for Regression stage -------------------------------

# Binary
Comply$comply_b<- ifelse(Comply[,2]>0,1,0)

# Ordered
lower <- Comply[,2]+Comply[,3]
upper <- Comply[,2]-Comply[,3]
comply_n <- matrix(NA,nrow(Comply),1)
comply_n <- ifelse(lower<0,0,NA)  			#Clear noncompliance
comply_n <- ifelse(upper>0,2, comply_n)		#Clear compliance
comply_n <- ifelse(lower*upper<0,1, comply_n)	#Ambiguous case
Comply$comply_n <- comply_n		#append to data
Comply$Dispute <- as.numeric(Comply$Dispute)

# Export DV -- Uncomment to Use
# write.csv(Comply, "WTO_SCMComply_5yrwindow_2019_ISQ.csv", row.names=F)